{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "MLutIRWTYLqq",
   "metadata": {
    "id": "MLutIRWTYLqq"
   },
   "source": [
    "# Simple Chat with Qwen2.5 (0.5B) using Hugging Face Transformers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2LoeTPnYQOW",
   "metadata": {
    "id": "a2LoeTPnYQOW"
   },
   "source": [
    "This beginner-friendly notebook demonstrates how to use the Qwen2.5-0.5B-Instruct model for simple chat-based generation. It includes installing dependencies, setting up authentication via Hugging Face token, generating a response using a chat prompt, and displaying the output. The notebook is organized into 5 clean sections for easy understanding."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "WlfJBFucY9gi",
   "metadata": {
    "id": "WlfJBFucY9gi"
   },
   "source": [
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Qwen2_5_InstructionAgent.ipynb)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57059c85",
   "metadata": {
    "id": "57059c85"
   },
   "source": [
    "# 🔧 Dependencies\n",
    "Install necessary Python packages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1cdaae47",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "1cdaae47",
    "outputId": "5b140b09-5c16-4849-f370-f8b5abcc127a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m363.4/363.4 MB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.8/13.8 MB\u001b[0m \u001b[31m58.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m24.6/24.6 MB\u001b[0m \u001b[31m58.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m883.7/883.7 kB\u001b[0m \u001b[31m39.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m1.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.5/211.5 MB\u001b[0m \u001b[31m6.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.3/56.3 MB\u001b[0m \u001b[31m14.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m127.9/127.9 MB\u001b[0m \u001b[31m9.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.5/207.5 MB\u001b[0m \u001b[31m6.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m21.1/21.1 MB\u001b[0m \u001b[31m64.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25h"
     ]
    }
   ],
   "source": [
    "!pip install transformers --quiet\n",
    "!pip install accelerate --quiet\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a65a2fa",
   "metadata": {
    "id": "9a65a2fa"
   },
   "source": [
    "# 🛠️ Tools\n",
    "Import required tools and libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a8a4bc30",
   "metadata": {
    "id": "a8a4bc30"
   },
   "outputs": [],
   "source": [
    "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
    "import torch\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ad2bec4",
   "metadata": {
    "id": "1ad2bec4"
   },
   "source": [
    "# ⚙️ YAML Prompt (Token Authentication)\n",
    "Use Hugging Face token to access the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cd030b94",
   "metadata": {
    "id": "cd030b94"
   },
   "outputs": [],
   "source": [
    "from huggingface_hub import login\n",
    "login(token=\"Enter your huggingface token\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7b1a32d",
   "metadata": {
    "id": "b7b1a32d"
   },
   "source": [
    "# 🚀 Main\n",
    "Load model, prepare prompt and generate response."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ef73b9ea",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 365,
     "referenced_widgets": [
      "a175e2b1fa8c4733afe1ddc78095be58",
      "f5aaf63d7be04927a33e5cdd3655a003",
      "7af74b8c184b4e72a2dba769b9caa40f",
      "22b1c604d91648479cf01198db25a863",
      "faaeb1c3ab914a6db9ff9b8ea06a440f",
      "138860a12c6347fba11437eb098d31b5",
      "d2f5fe459d544fa6adc92debab823ee6",
      "468e8e7bee5c4198b121ac1aa677153d",
      "068991c9ac44496bab06ccaa7d030722",
      "44436151662f41ef81f6a4005add51de",
      "c774c78c4fce4825872a76bb1e5fde33",
      "4a7d0a84994c4b628974f631ecb95425",
      "4adb654ea143456eb3b4244c3030b39f",
      "d8dbdbc9327f4799a07767b59db59c47",
      "669ea95c4cb642b580aa176ae2e89f9f",
      "0bc8529b03364f83adf1110b5c1378ec",
      "2eb8007145ef4c3caf7f5a9d1fd299e3",
      "3fa4c368cea4474e8b97a215a20fd8d8",
      "c750b36cfda24e24ae44a615e426de4b",
      "e3001e1e3ba74c7ba28702fa5b2a6ed8",
      "7ac0f7770ad341f68f194705f59ccb27",
      "f9140e1979c6412993eee36c3f79c0d9",
      "1ad8e2e2200c4277ba10dd8e9ac83b90",
      "f193e4fb00b64e86b6f40f10f514739d",
      "ae3aa36b649a43eb928ed843d2cfc239",
      "c346fba7badb4c4596f6d6e8fa62224b",
      "39dd29002d7b46848a29dfeb7405d9f9",
      "166e17a0be5a4aa8831101eeb07a8645",
      "60f9503b404945a3b99b5207190474f5",
      "c480a676b82b48a0aae3dc438fa02ba8",
      "d97b7d0765e84926b974b2c19156ae20",
      "b22ca5c038f7419aa8e03f5f9a54accc",
      "0387fba7ed044fbfb2a78c8d593c7b38",
      "d6a31c3cf026450088596db5917c3e4a",
      "4948c0014a95413b81848ae2b4a5fe7e",
      "377cbca7363a495ea131a1e49a702168",
      "a5eaad3284034beab09d212765d7ff78",
      "7ed393b7c7564ab1897446eba875a4da",
      "66d773bc8c7a40bc95dcba35052675cb",
      "ad196b0deb2144b083cf8fddcbcfd147",
      "09a99df922154a528f327bba0a47cb9a",
      "bc9918e686244eb5b5db78932600a9d6",
      "4fef54176f4b4a2d8d543a4bf04b416a",
      "48551fb8dd5140cbb2d792e72b5fbb0d",
      "955225e4206a4ee5a4284579935db920",
      "e2faef80a0034c5384daf8ba66c1b75d",
      "f27c3ec92bc74bacb3f86672b689370a",
      "ec03dfb79a26474e812606b2664c931b",
      "4705d2f502a74c3bb8eb8a06c3ba6889",
      "7d8c99f622c34229894024c9772aed84",
      "1e6e892b153c4c05b7de806ec674215f",
      "85ac6a67bb0944b5bb8713fcf302da45",
      "796771b887ce49119307725382f73bfd",
      "c686586c431f4c22b0f0c698a9b213b6",
      "239180fd12624e3a90188270b55ee218",
      "e04653090c7e454d9c5e520e7f313588",
      "992c7a4b44aa4feb8e13714fa922d705",
      "a0a238c78d2842d680be448a2cb20157",
      "6ab5ebed63674b5e87327a1623eba7f0",
      "a49887aa0fcd4b91a0d5d978b29c1e0a",
      "d9b2e4142dd14264b90829a5a4116613",
      "154b27bb2ba44d10bde5c96929b65cc9",
      "f0bf6206c31f49f2ad1ce97b99963011",
      "1762735cfbcf4706b1c0686d3c2926a0",
      "d5e3440785794f0f9ad4fd0650c2f682",
      "08019bdd70364fabbc222dca66db0bb1",
      "6f0b3a92d47c42dd815a670bb1c0f42c",
      "54554545ad5a4f39b649f3acff6d5cba",
      "bfd8f4ee8c414349a3695975e79c332e",
      "332fc944eef14a9c82f71a68a3857dea",
      "3b0b899fcfae451ba8ec3d8da176fcec",
      "8383df3f07bd427287ad05726737cc67",
      "67c267d7d1ec424593775bf8129e8ac9",
      "85a50e2cb406403988639e6d6ecf9dc1",
      "7251ed20382c4a60bae8549f68971043",
      "44fbc377fd6a4c5a910c00dcceb143af",
      "2d6d273941f04c13af598c53e067aae7"
     ]
    },
    "id": "ef73b9ea",
    "outputId": "34512fa7-527c-45cb-f3c7-b42d37e74980"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: \n",
      "The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
      "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
      "You will be able to reuse this secret in all of your notebooks.\n",
      "Please note that authentication is recommended but still optional to access public models or datasets.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a175e2b1fa8c4733afe1ddc78095be58",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer_config.json:   0%|          | 0.00/7.30k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4a7d0a84994c4b628974f631ecb95425",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "vocab.json:   0%|          | 0.00/2.78M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1ad8e2e2200c4277ba10dd8e9ac83b90",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "merges.txt:   0%|          | 0.00/1.67M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d6a31c3cf026450088596db5917c3e4a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer.json:   0%|          | 0.00/7.03M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "955225e4206a4ee5a4284579935db920",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config.json:   0%|          | 0.00/659 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e04653090c7e454d9c5e520e7f313588",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/988M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6f0b3a92d47c42dd815a670bb1c0f42c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "generation_config.json:   0%|          | 0.00/242 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model_name = \"Qwen/Qwen2.5-0.5B-Instruct\"\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_name,\n",
    "    torch_dtype=\"auto\",\n",
    "    device_map=\"auto\"\n",
    ")\n",
    "\n",
    "messages = [\n",
    "    {\"role\": \"system\", \"content\": \"You are Qwen, a helpful assistant.\"},\n",
    "    {\"role\": \"user\", \"content\": \"Explain photosynthesis in simple words.\"}\n",
    "]\n",
    "\n",
    "text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
    "inputs = tokenizer(text, return_tensors=\"pt\").to(model.device)\n",
    "\n",
    "outputs = model.generate(**inputs, max_new_tokens=100)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf70c765",
   "metadata": {
    "id": "bf70c765"
   },
   "source": [
    "# 📤 Output\n",
    "Print the model response."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d8f70a7b",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "d8f70a7b",
    "outputId": "559e9358-c2b7-4410-c1a6-1624640dfeae"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "system\n",
      "You are Qwen, a helpful assistant.\n",
      "user\n",
      "Explain photosynthesis in simple words.\n",
      "assistant\n",
      "Photosynthesis is the process by which plants, algae, and some bacteria convert light energy from the sun into chemical energy stored in glucose molecules, using carbon dioxide and water as reactants. This process occurs in chloroplasts within plant cells and involves two main stages: the light-dependent reactions and the light-independent reactions (also known as the Calvin cycle).\n",
      "\n",
      "In the light-dependent reactions, light energy is captured by pigments like chlorophyll, causing electrons to be excited and transferred to oxygen through a series\n"
     ]
    }
   ],
   "source": [
    "response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
    "print(response)\n"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
